<?php

namespace App\Transformers;

use App\Models\Company;
use League\Fractal\TransformerAbstract;

class CompanyTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['users', 'categories', 'area', 'products', 'filiales'];

    public function transform(Company $company)
    {
        return [
            'id' => $company->id,
            'name' => $company->name,
            'area_id' => (int) $company->area_id,
            'logo' => $company->logo,
            'introduction' => $company->introduction,
            'remark' => $company->remark,
            'forbidden' => (bool)$company->forbidden,
            'created_at' => $company->created_at->toDateTimeString(),
            'updated_at' => $company->updated_at->toDateTimeString(),
        ];
    }

    public function includeUsers(Company $company)
    {
        $users = $company->users;

        return $this->collection($users, new UserTransformer());
    }

    public function includeCategories(Company $company)
    {
        $categories = $company->categories;

        return $this->collection($categories, new CategoryTransformer());
    }

    public function includeProducts(Company $company)
    {
        $products = $company->products;

        return $this->collection($products, new ProductTransformer());
    }

    public function includeFiliales(Company $company)
    {
        $filiales = $company->filiales;

        return $this->collection($filiales, new ProductTransformer());
    }

    public function includeArea(Company $company)
    {
        return $this->item($company->area, new AreaTransformer());
    }
}